package com.kaiyun.sharding;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaiyun.sharding.mapper.CourseBaseMapper;
import com.kaiyun.sharding.mapper.CourseInfoMapper;
import com.kaiyun.sharding.mapper.DictionaryMapper;
import com.kaiyun.sharding.model.CourseBase;
import com.kaiyun.sharding.model.CourseInfo;
import com.kaiyun.sharding.model.Dictionary;
import com.kaiyun.sharding.model.param.Course;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * 测试公共表
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class ShardingApplicationTests4 {

    @Autowired
    private CourseInfoMapper courseInfoMapper;


    @Autowired
    private CourseBaseMapper courseBaseMapper;



    /**
     * 关联查询，sharding-jdbc不能够跨库的关联查询，
     * 所以关联表一定要和主表在一个数据库中
     */
    @Test
    public void getCourseInfoList() {
        Page<Course> page=new Page<>(1,2);
        //t_course_info 和t_user_Info 进行关联查询，参数为t_course_info的name
        // select a.name,b.name userName from t_course_info a left join t_user_info b
        // on a.user_id =b.id where a.name like concat('%',#{name},'%')  order by a.id desc
        List<Course> list = courseInfoMapper.getList(page, "姓名");
        list.forEach(System.out::println);
        log.info("总数："+page.getTotal());
    }


    /**
     * 多库关联查询
     */
    @Test
    public void getCourseBaseList() {
        Page<Course> page=new Page<>(1,100);
        CourseBase base=new CourseBase();
//        base.setUserId((long)1);
        base.setName("批量");

        //select a.name,b.name userName from t_course_base a left join t_dictionary b on a.user_id=b.user_id
        // where a.name like  concat('%',#{base.name},'%')  order by a.id desc
        List<Course> list = courseBaseMapper.getList4(page, base);
        list.forEach(System.out::println);
        log.info("总数："+page.getTotal());
    }


}
